#include "pebble_os.h"
#include "pebble_os.h"
#include "pebble_app.h"
#include "pebble_fonts.h"
#include "menu_{{menu_name|lower}}.h"
{%- for item in items %}
#include "menu_{{item|lower}}.h"
{%- endfor %}

static Window window;
static BmpContainer image;
static Layer layer;
static TextLayer text_layer;
static GFont custom_font_small;
static int displayedItem;

static const int IMAGE_RESOURCE_IDS[{{items|length}}] = {
	{%- for item in items %}
	RESOURCE_ID_IMAGE_{{ item|upper }}{% if not loop.last %},{% endif %}
	{%- endfor %}
};

static void update_text_display() {
	switch(displayedItem){
		{%- for item in items %}
		case {{ loop.index0 }}: text_layer_set_text(&text_layer, get_{{item|lower}}_str());  break;
		{%- endfor %}
	}
}

static void click_handler(ClickRecognizerRef recognizer, Window *window) {
	(void)recognizer;
	(void)window;

	ButtonId buttonId = click_recognizer_get_button_id(recognizer);	
	if( buttonId == BUTTON_ID_UP )
		displayedItem--;
	else if( buttonId == BUTTON_ID_DOWN )
		displayedItem++;
	else if( buttonId == BUTTON_ID_SELECT ) {
		switch(displayedItem){
		{%- for item in items %}
		case {{ loop.index0 }}: show_menu_{{item|lower}}();  break;
		{%- endfor %}
		}
		return;
	}

	if(displayedItem == {{items|length}})
		displayedItem--;
	else if(displayedItem == -1)
		displayedItem++;

	layer_remove_from_parent(&image.layer.layer);
    bmp_deinit_container(&image);

	bmp_init_container(IMAGE_RESOURCE_IDS[displayedItem], &image);
	layer_add_child(&layer, &image.layer.layer);

	update_text_display();
}

static void config_provider(ClickConfig **config, Window *window) {
	(void)window;

	config[BUTTON_ID_SELECT]->click.handler = (ClickHandler) click_handler;
  	config[BUTTON_ID_SELECT]->click.repeat_interval_ms = 100;

	config[BUTTON_ID_UP]->click.handler = (ClickHandler) click_handler;
	config[BUTTON_ID_UP]->click.repeat_interval_ms = 100;

	config[BUTTON_ID_DOWN]->click.handler = (ClickHandler) click_handler;
	config[BUTTON_ID_DOWN]->click.repeat_interval_ms = 100;
}

static void window_appear(Window* window) {
	update_text_display();
}

static void window_load(Window* window) {
	bmp_init_container(IMAGE_RESOURCE_IDS[displayedItem], &image);
	layer_add_child(&layer, &image.layer.layer);
}

static void window_unload(Window* window) {
	bmp_deinit_container(&image);
}

void show_menu_{{menu_name|lower}}(int firstItem){
	displayedItem = firstItem;

	window_init(&window, "{{menu_name|capitalize}}");
	window_set_window_handlers(&window, (WindowHandlers){
		.unload = window_unload,
		.load = window_load,
		.appear = window_appear,
	});

	resource_init_current_app(&GOPRO_RESOURCES);

	custom_font_small = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_COMFORTAA_20));

	window_stack_push(&window, true);
	window_set_background_color(&window, GColorBlack);

    // Attach our desired button functionality
	window_set_click_config_provider(&window, (ClickConfigProvider) config_provider);

	// Attach our desired button functionality
	window_set_click_config_provider(&window, (ClickConfigProvider) config_provider);

	layer_init(&layer, GRect(17, 20, 110 /* width */, 100 /* height */));
    layer_add_child(&window.layer, &layer);

    text_layer_init(&text_layer, GRect(0, 130, 144 /* width */, 20 /* height */));
    text_layer_set_text_color(&text_layer, GColorWhite);
    text_layer_set_background_color(&text_layer,GColorBlack); 
    text_layer_set_text_alignment(&text_layer, GTextAlignmentCenter);
    text_layer_set_font(&text_layer,custom_font_small);

    layer_add_child(&window.layer, &text_layer.layer);
}

const char* get_{{ menu_name }}_str(){
    return "";
}